<?php

namespace App\Console\Commands;

use App\Model\Union;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class UserDeviceRepair extends Command
{
    /**
     * 用户缺失设备信息修复
     */
    protected $signature = 'UserDeviceRepair';

    /**
     * 
     * The console command description.
     *
     * @var string
     */
    protected $description = '用户缺失设备信息修复';
    protected $connection = 'api'; 

    protected $user_table = 'user'; //用户表
    protected $user_device_table = 'device';//登录设备表
    protected $user_register_log_table = 'user_register_log';//用户注册表记录

    protected $log = 'repair_device';//修复设备信息成功日志
    protected $errorLog = 'error_repair_device';//修复设备信息失败日志
    

    
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct(); 
    }

    /**
     *
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {    
        //查询用户表数据
        $userArr = DB::connection($this->connection)->table($this->user_table)->whereNotIn('user_id',[10,11,12,13,14,9])->get(['user_id','guid']);
        $userArr = objToArray($userArr);
        if(empty($userArr))
        {
            write_log($this->log,'INFO 无数据',[]);
            die;
        }
        foreach($userArr as $v){
            try{
                DB::beginTransaction();
                //查询设备表里是否有数据
                $deviceStatus = DB::connection($this->connection)->table($this->user_device_table)->where('guid',$v['guid'])->exists();
                if($deviceStatus)
                {
                    continue;
                }
                //没有设备信息去注册log中取信息
                $registerLog = DB::connection($this->connection)->table($this->user_register_log_table)->where('user_id',$v['user_id'])->first();
                $registerLog = objToArray($registerLog);
                //预备数据
                $device['guid'] = $v['guid'];//设备标识
                $device['version_no'] = $registerLog['version_no'];//app版本信息
                $device['os_name'] = $registerLog['os_name'];//设备类型
                $device['device'] = $registerLog['device'];//系统版本号
                $device['channel_id'] = $registerLog['channel_id'];//渠道来源
                $device['created_at'] = $registerLog['time'];//注册时间
                $device['platform'] = $registerLog['platform'];//渠道标识
                $device['aid'] = $registerLog['aid'];//广告id
                $device['ua'] = $registerLog['ua'];//自定义的UA
                $device['client_ip'] = $registerLog['client_ip'];//客户端IP

                //插入表
                $res = DB::connection($this->connection)->table($this->user_device_table)->insertGetId($device);
                write_log($this->log,'INFO 新增用户'.$v['user_id'].'设备信息',$res);
                DB::commit();
            }catch (\Exception $e) {
                DB::rollBack();
                write_log($this->errorLog, "新增设备错误信息", '发送错误文件===>'.$e->getFile().PHP_EOL."发生错误行===>".$e->getLine().PHP_EOL.'发生错误信息====>'.$e->getMessage());
            }
        }    
    }
    
}
